home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 April: Mac OS SDK / Dev.CD Apr 96 SDK / Dev.CD Apr 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc / Sample Code / Sample Editors⁄Viewers / Cappuccino / Source / RefCounted.cpp < prev    next >
Encoding:
Text File  |  1995-12-11  |  3.8 KB  |  161 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:            RefCounted.cpp
  3.     
  4.     Contains:        C++ mix-in class for doing reference counting.
  5.  
  6.     Written by:        Troy Gaul
  7.     
  8.     Copyright:        © 1995 by Apple Computer, Inc., all rights reserved.
  9. */
  10.  
  11. // -- Compiler/Preprocessor Switches --
  12.  
  13. #ifndef _COMPILERDEFS_
  14. #include "CompDefs.h"
  15. #endif
  16.  
  17. // -- OpenDoc Utilities --
  18.  
  19. #ifndef _EXCEPT_
  20. // Exceptions define several important macros (eg. CHECKENV)
  21. // which are used in the SOM method dispatch glue. If Except.h
  22. // is not included early enough, exceptions may not be thrown
  23. // correctly when returning from a SOM method with the "ev" parameter set.
  24. #include <Except.h>
  25. #endif
  26.  
  27. // -- Utilites --
  28.  
  29. #ifndef _REFCOUNTED_
  30. #include "RefCounted.h"
  31. #endif
  32.  
  33. // -- OpenDoc Utilites --
  34.  
  35. #ifndef _ODDEBUG_
  36. #include "ODDebug.h"
  37. #endif
  38.  
  39. //------------------------------------------------------------------------------
  40. // Debugging
  41. //------------------------------------------------------------------------------
  42. #if ODDebug
  43.  
  44. ODULong sNumRefCounted = 0;
  45.  
  46. void CheckRefCountedObjects()
  47. {
  48.     if ( sNumRefCounted != 0)
  49.         WARN("There are %ld remaining RefCounted objects!", sNumRefCounted);
  50. }
  51.  
  52. #endif
  53.  
  54.  
  55. //==============================================================================
  56. // MRefCounted
  57. //==============================================================================
  58.  
  59. //------------------------------------------------------------------------------
  60. // Constructor
  61. //------------------------------------------------------------------------------
  62.  
  63. MRefCounted::MRefCounted()
  64. {
  65.     fRefCount = 1;
  66.  
  67. #if ODDebug
  68.     sNumRefCounted++;
  69. #endif
  70. }
  71.  
  72. //------------------------------------------------------------------------------
  73. // Destructor
  74. //------------------------------------------------------------------------------
  75.  
  76. MRefCounted::~MRefCounted()
  77. {
  78. #if ODDebug
  79.     if ( fRefCount != 0 )
  80.         WARN("MRefCounted deleted with ref-count of %ld.", fRefCount);
  81.  
  82.     sNumRefCounted--;
  83. #endif
  84. }
  85.  
  86. //------------------------------------------------------------------------------
  87. // Acquire
  88. //------------------------------------------------------------------------------
  89.  
  90. void MRefCounted::Acquire()
  91. {
  92.     fRefCount++; 
  93. }
  94.  
  95. //------------------------------------------------------------------------------
  96. // Release
  97. //------------------------------------------------------------------------------
  98.  
  99. void MRefCounted::Release()
  100. {
  101.     WASSERTM(fRefCount > 0, "MRefCounted released with refcount < 1.");
  102.     fRefCount--;
  103.     
  104.     if ( fRefCount == 0 )
  105.         delete this;
  106. }
  107.  
  108. //------------------------------------------------------------------------------
  109. // GetRefCount
  110. //------------------------------------------------------------------------------
  111.  
  112. ODSLong MRefCounted::GetRefCount()
  113. {
  114.     return fRefCount;
  115. }
  116.  
  117. //==============================================================================
  118. // TempRefCounted
  119. //==============================================================================
  120.  
  121. //------------------------------------------------------------------------------
  122. // Constructor
  123. //------------------------------------------------------------------------------
  124.  
  125. TempRefCounted::TempRefCounted( MRefCounted* object )
  126. {
  127.     fObject = object;
  128. }
  129.  
  130. //------------------------------------------------------------------------------
  131. // Destructor
  132. //------------------------------------------------------------------------------
  133.  
  134. TempRefCounted::~TempRefCounted()
  135. {
  136.     if ( fObject != kODNULL )
  137.     {
  138.         fObject->Release();
  139.         fObject = kODNULL;
  140.     }
  141. }
  142.  
  143. //------------------------------------------------------------------------------
  144. // operator=
  145. //------------------------------------------------------------------------------
  146.  
  147. MRefCounted* TempRefCounted::operator=( MRefCounted* object )
  148. {
  149.     fObject = object;
  150.     return object;
  151. }
  152.  
  153. //------------------------------------------------------------------------------
  154. // DontRelease
  155. //------------------------------------------------------------------------------
  156.  
  157. void TempRefCounted::DontRelease()
  158. {
  159.     fObject = kODNULL;
  160. }
  161.